$(function() {
	var startDateDefault = moment().subtract('days', 6);// 7 days ago
	var endDateDefaul = moment();
	var today = moment();
	var yestoday = moment().subtract('days', 1);
	var sevenDay = moment().subtract('days', 6);
	var Day29 = moment().subtract('days', 29);
	var thisMonthBegin = moment().startOf('month');
	var thisMonthEnd = moment().endOf('month');
	var lastMonthBegin = moment().subtract('month', 1).startOf('month');
	var lastMonthEnd = moment().subtract('month', 1).endOf('month');

	var cb = function(start, end, label) {
		$('#timeRange span').html(
				start.format('YYYY/MM/DD') + ' - ' + end.format('YYYY/MM/DD'));
	};

	var optionSet1 = {
		startDate : startDateDefault,
		endDate : endDateDefaul,
		dateLimit : {
			days : 31
		},
		showDropdowns : true,
		showWeekNumbers : true,
		timePicker : false,
		timePickerIncrement : 1,
		timePicker12Hour : true,
		ranges : {
			'Today' : [ today, today ],
			'Yestoday' : [ yestoday, yestoday ],
			'Last 7 days' : [ sevenDay, today ],
			'Last 30 days' : [ Day29, today ],
			'This Month' : [ thisMonthBegin, thisMonthEnd ],
			'Last Month' : [ lastMonthBegin, lastMonthEnd ]
		},
		opens : 'right',
		buttonClasses : [ 'btn btn-default' ],
		applyClass : 'btn-small btn-primary',
		cancelClass : 'btn-small',
		format : 'YYYY-MM-DD',
		separator : ' to ',
		locale : {
			applyLabel : 'Apply',
			cancelLabel : 'Cancel',
			fromLabel : 'From',
			toLabel : 'To',
			customRangeLabel : 'Custom',
			daysOfWeek : [ 'Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa' ],
			monthNames : [ 'January', 'February', 'March', 'April', 'May',
					'June', 'July', 'August', 'September', 'October',
					'November', 'December' ],
			firstDay : 1
		}
	};

	$('#timeRange span').html(
			startDateDefault.format('YYYY/MM/DD') + ' - '
					+ endDateDefaul.format('YYYY/MM/DD'));
	drawTable(startDateDefault, endDateDefaul);

	$('#timeRange').daterangepicker(optionSet1, cb);
	$('#timeRange').on('apply.daterangepicker', function(ev, picker) {
		drawTable(picker.startDate, picker.endDate);
	});
	$('#timeRange').on('cancel.daterangepicker', function(ev, picker) {
		console.log("cancel event fired");
	});

	function drawTable(startDate, endDate) {
		var start = (startDate).format('YYYY-MM-DD');
		$(".table thead tr").html("<th>Carrier&nbsp;&nbsp;&nbsp;&nbsp;</th>");
		$(".table tbody").html("");
		sum = parseInt((endDate - startDate) / (1000 * 60 * 60 * 24));
		var date = moment(startDate.format('YYYY-MM-DD'), 'YYYY-MM-DD');
		var eDate = moment(endDate.format('YYYY-MM-DD'), 'YYYY-MM-DD');
		var end = (eDate.add('days', 1)).format('YYYY-MM-DD');
		var theadContent;
		while (date.diff(eDate, 'days') < 0) {
			theadContent = date.format('YYYY-MM-DD');
			$(".table thead tr").append(
					"<th>" + theadContent + "&nbsp;&nbsp;&nbsp;&nbsp;</th>");
			date.add('days', 1);
		}

		$
				.ajax({
					url : window.basePath + "log/carrierList",
					type : 'POST',
					data : {
						start : start,
						end : end
					},
					success : function(data) {
						for ( var i = 0; i < data.length; i++) {
							var td = "";
							var k = 0;
							$("#carrierAnalysis thead tr th")
									.each(
											function() {
												if (k > 0) {
													var flag = 0;
													for ( var j = 0; j < data[i].info.length; j++) {
														var thHtml = $(this)
																.html()
																.split("&nbsp;");
														if (thHtml[0] == data[i].info[j].date) {
															var valueF = parseFloat(data[i].info[j].value);
															if (valueF > 0) {
																td += "<td class='warning'>"
																		+ data[i].info[j].value
																		+ "%";
															} else {
																td += "<td>0";
															}

															td += "</td>";
															flag = 1;
														}
													}
													if (flag == 0) {
														td += "<td>0</td>";
													}
												}

												k = k + 1;
											});

							$("#carrierAnalysis tbody").append(
									"<tr><td>" + data[i].carrier + "</td>" + td
											+ "<tr/>");
						}
						$("#carrierAnalysis").tablesorter({
							widgets : [ 'zebra' ],
						// widthFixed: true
						});
					}
				});

		$
				.ajax({
					url : window.basePath + "log/carrierDfa",
					type : 'POST',
					data : {
						start : start,
						end : end
					},
					success : function(data) {
						for ( var i = 0; i < data.length; i++) {
							var td = "";
							var k = 0;
							$("#carrierDfaAnalysis thead tr th")
									.each(
											function() {
												if (k > 0) {
													var flag = 0;
													for ( var j = 0; j < data[i].info.length; j++) {
														var thHtml = $(this)
																.html()
																.split("&nbsp;");
														if (thHtml[0] == data[i].info[j].date) {
															var valueF = parseFloat(data[i].info[j].value);
															if (valueF > 0) {
																td += "<td class='warning'>"
																		+ data[i].info[j].value
																		+ "%";
															} else {
																td += "<td>0";
															}

															td += "</td>";
															flag = 1;
														}
													}
													if (flag == 0) {
														td += "<td>0</td>";
													}
												}

												k = k + 1;
											});

							$("#carrierDfaAnalysis tbody").append(
									"<tr><td>" + data[i].carrier + "</td>" + td
											+ "<tr/>");
						}
						$("#carrierDfaAnalysis").tablesorter({
							widgets : [ 'zebra' ]
						});
					}
				});

		$
				.ajax({
					url : window.basePath + "log/carrierFt",
					type : 'POST',
					data : {
						start : start,
						end : end
					},
					success : function(data) {
						for ( var i = 0; i < data.length; i++) {
							var td = "";
							var k = 0;
							$("#carrierFtAnalysis thead tr th")
									.each(
											function() {
												if (k > 0) {
													var flag = 0;
													for ( var j = 0; j < data[i].info.length; j++) {
														var thHtml = $(this)
																.html()
																.split("&nbsp;");
														if (thHtml[0] == data[i].info[j].date) {
															var valueF = parseFloat(data[i].info[j].value);
															if (valueF > 0) {
																td += "<td class='warning'>"
																		+ data[i].info[j].value
																		+ "%";
															} else {
																td += "<td>0";
															}

															td += "</td>";
															flag = 1;
														}
													}
													if (flag == 0) {
														td += "<td>0</td>";
													}
												}
												k = k + 1;
											});

							$("#carrierFtAnalysis tbody").append(
									"<tr><td>" + data[i].carrier + "</td>" + td
											+ "<tr/>");
						}
						$("#carrierFtAnalysis").tablesorter({
							widgets : [ 'zebra' ]
						});
					}
				});
	}

});
